/*
Copyright 2008, Mark Arvidson

This file is part of EwaScorecard.

EwaScorecard is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

EwaScorecard is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with EwaScorecard.  If not, see <http://www.gnu.org/licenses/>.
 */

import javax.microedition.lcdui.*;
import javax.microedition.rms.*;

public class SaveMeetDataForm extends Form implements CommandListener {

    private TextField filename;
    private Command backCommand = new Command("Cancel", Command.BACK, 0);
    private Command okCommand = new Command("OK", Command.OK, 0);
    private MeetData meetData;
    private EwaScorecard host;

    public SaveMeetDataForm(EwaScorecard host, MeetData data) {
        super("Save meet data");
        this.meetData = data;
        this.host = host;
        filename = new TextField("Filename", data.getMeetName(), 25, TextField.ANY);
        append(filename);
        addCommand(backCommand);
        addCommand(okCommand);
        setCommandListener(this);
    }

    public static void saveMeet(EwaScorecard host, MeetData dataToStore) {
        try {
            dataToStore.storeRecord(dataToStore.getFilename());
        } catch (RecordStoreFullException e) {
            Alert alert = new Alert("Could not save", "Out of memory", null, AlertType.ERROR);
            Display.getDisplay(host).setCurrent(alert);
        } catch (Exception ex) {
            Alert alert = new Alert("Could not save", ex.getMessage(), null, AlertType.ERROR);
            Display.getDisplay(host).setCurrent(alert);
        }
    }

    public void commandAction(Command c, Displayable d) {
        if (c == okCommand) {
            meetData.setFilename(filename.getString());
            saveMeet(host, meetData);
            host.mainMenu();

        } else if (c == backCommand) {
            host.mainMenu();
        }
    }
}
